package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOpeartion {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name = scanner.nextLine();
        int pos = -1;
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if (pos == -1){
            System.out.println("没有这本书");
            return;
        }
        int curSize = bookList.getUsedSize();
        int size = curSize-1;
        for (int i = pos; i < size; i++) {
            //Book book = bookList.getBook(i+1);
            //bookList.setBooks(i,book);
            bookList.setBooks(i,bookList.getBook(i+1));
        }
        bookList.setUsedSize(curSize-1);
        bookList.setBooks(curSize-1,null);

    }
}
